package com.ruoyi.project.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.common.datascope.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.YhEquipmentMapper;
import com.ruoyi.project.domain.YhEquipment;
import com.ruoyi.project.service.IYhEquipmentService;

import javax.validation.Validator;

/**
 * 设备信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-09
 */
@Service
public class YhEquipmentServiceImpl implements IYhEquipmentService 
{
    @Autowired
    private YhEquipmentMapper yhEquipmentMapper;

    @Autowired
    protected Validator validator;

    /**
     * 导入设备台账数据
     *
     * @param mapList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importContract(List<YhEquipment> mapList, Boolean isUpdateSupport)
    {

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (YhEquipment map  : mapList) {
            try {
                YhEquipment c = yhEquipmentMapper.selectYhEquipmentById(map.getId());
                if (StringUtils.isNull(c)) {
                    BeanValidators.validateWithException(validator, map);
                    this.insertYhEquipment(map);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 导入成功");

                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, map);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }




    /**
     * 查询设备信息
     * 
     * @param id 设备信息主键
     * @return 设备信息
     */
    @Override
    public YhEquipment selectYhEquipmentById(Long id)
    {
        return yhEquipmentMapper.selectYhEquipmentById(id);
    }

    /**
     * 查询设备信息列表
     * 
     * @param yhEquipment 设备信息
     * @return 设备信息
     */
    @Override
    @DataScope(deptAlias = "yh_equipment", userAlias = "yh_equipment")
    public List<YhEquipment> selectYhEquipmentList(YhEquipment yhEquipment)
    {
        return yhEquipmentMapper.selectYhEquipmentList(yhEquipment);
    }

    /**
     * 新增设备信息
     * 
     * @param yhEquipment 设备信息
     * @return 结果
     */
    @Override
    public int insertYhEquipment(YhEquipment yhEquipment)
    {
        yhEquipment.setCreateTime(DateUtils.getNowDate());
        return yhEquipmentMapper.insertYhEquipment(yhEquipment);
    }

    /**
     * 修改设备信息
     * 
     * @param yhEquipment 设备信息
     * @return 结果
     */
    @Override
    public int updateYhEquipment(YhEquipment yhEquipment)
    {
        yhEquipment.setUpdateTime(DateUtils.getNowDate());
        return yhEquipmentMapper.updateYhEquipment(yhEquipment);
    }

    /**
     * 批量删除设备信息
     * 
     * @param ids 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deleteYhEquipmentByIds(Long[] ids)
    {
        return yhEquipmentMapper.deleteYhEquipmentByIds(ids);
    }

    /**
     * 删除设备信息信息
     * 
     * @param id 设备信息主键
     * @return 结果
     */
    @Override
    public int deleteYhEquipmentById(Long id)
    {
        return yhEquipmentMapper.deleteYhEquipmentById(id);
    }

    /**
     * 通过设备类型查询设备名称
     *
     * @param equipmentType  设备类型
     * @return 设备信息
     */
    @Override
    public List<YhEquipment> selectYhEquipmentByType(String equipmentType)
    {
        return yhEquipmentMapper.selectYhEquipmentByType(equipmentType);
    }

}
